<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/25 0025
 * Time: 下午 2:44
 */

class Upload extends CI_Controller
{
    public function uploadfile()
    {
        header('Content-type: application/json');
        $date = date("y-m");
        $base_dir = sprintf("%s/%s", UPLOADPATH, $date);
        if (!file_exists($base_dir)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($base_dir, 0700);
        }
        $config['upload_path']      = $base_dir;
        $config['allowed_types']    = 'gif|jpg|jpeg|png';
        $config['max_size']     = 1024*5;
        $config['max_width']        = 1024*5;
        $config['max_height']       = 768*5;

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('file'))
        {
            $error = array('error' => $this->upload->display_errors());
            return t_(json_encode([
                "code" => 1005,
                "msg" => "error",
                "data" => $error
            ]));
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            if(isset($data["upload_data"])){
                $file_name = md5(time()).$data["upload_data"]['file_name'];
                $file_name = "/uploads/".$file_name;
            }
            return t_(json_encode([
                "code" => 1000,
                "msg" => "success",
                "data" => $file_name
            ]));
        }
    }
    public function upload_by_wexin(){
        header('Content-type: application/json');
        define("WEIXIN_APPID", HAO_WEIXIN_APPID);
        define("WEIXIN_APPSECRET", HAO_WEIXIN_APPSECRET);
        define("WEIXIN_MCHID", HAO_WEIXIN_MCHID);
        define("WEIXIN_KEY", HAO_WEIXIN_KEY);
        define("WEIXIN_SSLCERT_PATH", HAO_WEIXIN_KEY);
        define("WEIXIN_SSLKEY_PATH", HAO_WEIXIN_KEY);
        $this->load->library('WxPayPubHelper/SDKRuntimeException.php');
        $this->load->library('WxPayPubHelper/WxPaypubconfig.php');
        $this->load->library('WxPayPubHelper/WxPayPubHelper.php');
        //使用jsapi接口
        $jsApi = new \JsApi_pub();
        $access_token = $jsApi->access_token();
        if($access_token  === false){
            return t_(json_encode([
                "code" => 1005,
                "msg" => "access_token invalid",
                "data" => $access_token
            ]));
        }
        $serverId = $this->input->get('serverId');
        if(empty($serverId)){
            return t_(json_encode([
                "code" => 1004,
                "msg" => "serverId invalid",
                "data" => $serverId
            ]));
        }
        $str = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$serverId}";
        $arrContextOptions=array(
            "ssl"=>array(
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ),
        );
        $content = file_get_contents($str, false, stream_context_create($arrContextOptions));
        if($content){
            $date = date("y-m");
            $base_dir = sprintf("%s/%s", UPLOADPATH, $date);
            if (!file_exists($base_dir)) {
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                mkdir($base_dir, 0700);
            }
            $path = sprintf("%s/%s", $base_dir, $serverId);
            file_put_contents($path,$content);
            $x_path = sprintf("/uploads/%s/%s", $date, $serverId);
            return t_(json_encode([
                "code" => 1000,
                "msg" => "success",
                "data" => $x_path
            ]));
        }
        return t_(json_encode([
            "code" => 1006,
            "msg" => "error",
            "data" => ""
        ]));
    }
    public function uploadfile_1()
    {
        header('Content-type: application/json');
        $base64data = $this->input->post('data');

        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64data, $result)) {
            $type = $result[2];
            $date = date("y-m");
            $base_dir = sprintf("%s/%s", UPLOADPATH, $date);
            if (!file_exists($base_dir)) {
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                mkdir($base_dir, 0700);
            }
            $key = md5($base64data);
            $path = sprintf("%s/%s.%s", $base_dir, $key, $type);
            file_put_contents($path,
                base64_decode(
                    str_replace($result[1], '', $base64data)
                )
            );
            $x_path = sprintf("/uploads/%s/%s.%s", $date, $key,$type);
            return t_(json_encode([
                "code" => 1000,
                "msg" => "success",
                "data" => $x_path
            ]));
        } else {
            return t_(json_encode([
                "code" => 1005,
                "msg" => "success",
                "data" => ""
            ]));
        }
    }
}